import { userLogin, userRegister, userQuery, sendVerificationCode} from '@/api/userLogin'
import { Message } from 'element-ui';
import router from '@/router'
const user = {
    namespaced: true,
    state: {
        username: "",
        userRole: "未登录",
        userPhone: null,
        token: {
          token: '',
          lastTime: 0
        },
    },
  
    getters: {
      getUserphone: (state) => {
        return state.userPhone;
      },
      getUserRole: (state) => {
        return state.userRole;
      }
    },
  
    mutations: {
      SET_DATA: (state, payload) => { // 从localStorage同步
        state.userPhone = payload.userPhone;
        state.token = payload.token;
        state.username = payload.username;
        state.userRole = payload.userRole;
      },
      SET_USERPHONE: (state, userPhone)=>{
        state.userPhone = userPhone;
      },
      SET_USERNAME: (state, username)=>{
        state.username = username;
      },
      SET_USERROLE: (state, userRole)=>{
        state.userRole = userRole;
      },
      SET_TOKEN: (state, token)=>{
        const input = {};
        input.token = token;
        input.lastTime = new Date().getTime();
        state.token = input;
      }
    },
  
    actions: {
      login: async(context, {userPhone, password})=>{
        return new Promise((resolve, reject) => {
          userLogin(userPhone, password)
          .then(res=>{
            if (res.locked) {
              context.commit("SET_TOKEN", null);
              Message.error("您的帐户已被锁定,请联系管理修改");
            } else{
              context.commit("SET_USERPHONE", res.userPhone);
              context.commit("SET_USERNAME", res.name);
              context.commit("SET_USERROLE", res.role);
              Message.success(res.name + "登录成功");
              if (res.role === 'USER') {
                router.push({ name: 'Home'});
              } else if (res.role === 'ORGAN_ADMIN') {
                router.push({ name: 'AppoimentManage'});
              } else if (res.role === 'SYSTEM_ADMIN') {
                router.push({ name: 'HospitalManage'});
              }
            }
            resolve();
          }).catch(res => {
            reject(res);
          });
        });
      },
      logout: (context)=>{
        context.commit("SET_USERPHONE", null);
        context.commit("SET_TOKEN", null);
        context.commit("SET_USERROLE", "未登录");
        Message.success("退出登录成功");
      },
      register: async(context, userInfo)=>{
        return new Promise((resolve, reject) => {
          userRegister(userInfo)
          .then(res=>{
            Message.success("注册用户成功");
            router.push({name: "Login"});
            resolve();
          }).catch(res => {
            reject(res);
          });
        })
      },
      userQuery: ()=>{
        userQuery()
        .then(res=>{
          console.log(res);
        })
      },
      send_verification_code: (context, userPhone)=>{
        sendVerificationCode(userPhone)
        .then(res=>{
          Message.success("验证码发送成功");
        })
      }
    }
  };
  
  export default user;
  